package com.yaozheng.vocationaltraining.service.impl;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.ICategoriesView;
import com.yaozheng.vocationaltraining.service.CategoriesService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

/**
 */
@EBean
public class CategoriesServiceImpl implements CategoriesService {
    ICategoriesView iCategoriesView;

    @Override
    public void init(ICategoriesView iCategoriesView) {
        this.iCategoriesView = iCategoriesView;
    }

    @Background
    @Override
    public void categoriesList(final int id) {
        try {
            OkHttpClientManager.getInstance().get(
                    Constants.ROOTURL + "/course/categories?parentId=" + id,
                    iCategoriesView.getToken(), new BaseView(iCategoriesView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iCategoriesView.categoriesListSuccess(jsonObject, id);
                        }

                        @Override
                        public void responseError(String error) {
                            iCategoriesView.categoriesListError(error, id);
                        }
                    });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iCategoriesView.isResponseResult()) {
                iCategoriesView
                        .categoriesListError(ErrorUtils.SERVER_CONNECTION_ERROR, id);
            }

        }
    }
}
